package com.invest.task.jsl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.invest.common.config.JslConfig;
import com.invest.common.utils.StringUtils;
import com.invest.domain.entity.CbData;
import com.invest.mapper.CbDataMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class CbDataJsl {

    @Autowired
    private CbDataMapper cbDataMapper;

    @Autowired
    private GetJslCookie getJslCookie;

    @Autowired
    private JslConfig jslConfig;



    public void startJsl() throws IOException {

        String cookie = getJslCookie.getCookie();
        String cbUrl = jslConfig.getCbUrl();
        log.info("登录cookies ： " + cookie);
        if(!StringUtils.isEmpty(cookie)&& !StringUtils.isEmpty(cbUrl)){
            getData(cbUrl, cookie);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void getData(String url, String cookies) throws IOException {

        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 创建HttpGet请求
            HttpGet request = new HttpGet(url);
            request.setHeader("Accept", "application/json, text/plain, */*");
            request.setHeader("Accept-Encoding", "gzip, deflate, br, zstd");
            request.setHeader("Accept-Language", "en,en-GB;q=0.9,en-US;q=0.8,zh-CN;q=0.7,zh;q=0.6");
            request.setHeader("Columns", "1,70,2,3,5,6,11,12,14,15,16,29,30,32,34,44,46,47,50,52,53,54,56,57,58,59,60,62,63,67");
            request.setHeader("Cookie",cookies );
            request.setHeader("If-Modified-Since", "Tue, 14 Jan 2025 08:03:36 GMT");
            request.setHeader("Init", "1");
            request.setHeader("Priority", "u=1, i");
            request.setHeader("Referer", jslConfig.getCbReferer());
            request.setHeader("Sec-Ch-Ua", "\"Google Chrome\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"");
            request.setHeader("Sec-Ch-Ua-Mobile", "?0");
            request.setHeader("Sec-Ch-Ua-Platform", "\"Windows\"");
            request.setHeader("Sec-Fetch-Dest", "empty");
            request.setHeader("Sec-Fetch-Mode", "cors");
            request.setHeader("Sec-Fetch-Site", "same-origin");
            request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36");

            // 发送请求
            try (CloseableHttpResponse response = httpClient.execute(request)) {
                // 获取响应状态码
                int statusCode = response.getStatusLine().getStatusCode();
                if(statusCode != 200){
                    //todo  获取请求出错，发送邮件处理
                    return;
                }
                // 获取响应体
                if (response.getEntity()!=null) {
                    String responseBody = EntityUtils.toString(response.getEntity());
                    JSONObject jsonObject = JSON.parseObject(responseBody);
                    Object o = jsonObject.get("data");
                    List<CbData> cbDataList = JSON.parseArray(o.toString(), CbData.class);
                    log.info("获取的可转债数据量----->" + cbDataList.size());
                    ArrayList<CbData> list = new ArrayList<>();
                    for (CbData cbData : cbDataList) {
                        if (!StringUtils.isEmpty(cbData.getBondId())) {
                            cbData.setMainUpdateTime(new Date());
                            list.add(cbData);
                        }
                    }
                    // 应该先判断转债是否在库中存在 不存在就插入    存在就更新
                    cbDataMapper.deleteAll( );
                    cbDataMapper.insertCbDataBatch(list);

                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
